/**
 * @Copyright 
 * @Project test4jweb
 * @Package wxyz.test4jweb.serlvet
 * @CreateTime 2016-1-26 上午10:36:13
 */
package wxyz.test4jweb.serlvet;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import wxyz.test4jweb.utils.Logger;

/**
 * <p>
 * 测试通过nginx后，各个参数是否正常
 * </p>
 * @Author wxyz
 * @CreateTime 2016-1-26 上午10:36:13 init class
 */
public class TestNginxServlet extends HttpServlet {
	private static final long serialVersionUID = 2336845430894992576L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("text/html; charset=utf-8");
		
		Logger.debug(getClass(), req.getContextPath() + req.getServletPath());
		
		PrintWriter pw = resp.getWriter();
		pw.println("hello servlet");
		pw.print("<br>");
		
		Cookie[] cookies = req.getCookies();
		if (null != cookies) {
			for (Cookie c : cookies) {
				pw.println(c.getName() + "=" + c.getValue() + ", " + c.getMaxAge());
				pw.print("<br>");
			}
		}
		else {
			pw.println("没有cookies");
		}
		
		pw.println("客户端IP：" + getIpAddr(req));
		pw.print("<br>");
		
		pw.flush();
		pw.close();
	}
	
	public String getIpAddr(HttpServletRequest req) {
		String ip = req.getHeader("x-forwarded-for");
		Logger.debug(getClass(), "x-forwarded-for: " + ip);
		if (null == ip || 0 == ip.length() || "unknown".equalsIgnoreCase(ip)) {
			ip = req.getHeader("Proxy-Client-IP");
			Logger.debug(getClass(), "Proxy-Client-IP: " + ip);
		}
		if (null == ip || 0 == ip.length() || "unknown".equalsIgnoreCase(ip)) {
			ip = req.getHeader("WL-Proxy-Client-IP");
			Logger.debug(getClass(), "WL-Proxy-Client-IP: " + ip);
		}
		if (null == ip || 0 == ip.length() || "unknown".equalsIgnoreCase(ip)) {
			ip = req.getHeader("HTTP_CLIENT_IP");
			Logger.debug(getClass(), "HTTP_CLIENT_IP: " + ip);
		}
		if (null == ip || 0 == ip.length() || "unknown".equalsIgnoreCase(ip)) {
			ip = req.getHeader("HTTP_X_FORWARDED_FOR");
			Logger.debug(getClass(), "HTTP_X_FORWARDED_FOR: " + ip);
		}
		if (null == ip || 0 == ip.length() || "unknown".equalsIgnoreCase(ip)) {
			ip = req.getRemoteAddr();
			if (ip.equals("127.0.0.1") || ip.equals("0:0:0:0:0:0:0:1")) {
				InetAddress inet = null;
				try {
					inet = InetAddress.getLocalHost();
					ip = inet.getHostAddress();
				}
				catch (UnknownHostException e) {
					e.printStackTrace();
					Logger.warn(getClass(), "客户端IP地址获取失败。", e);
				}
			}
			Logger.debug(getClass(), "getRemoteAddr: " + ip);
		}
		
		
		if (null != ip) {
			ip = ip.split(",")[0];
		}
		
		return ip;
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doPost(req, resp);
	}
}
